package com.ruoyi.special.service.impl;

import cn.hutool.core.util.ObjectUtil;
import cn.hutool.core.util.StrUtil;
import com.ruoyi.common.annotation.DataSource;
import com.ruoyi.common.enums.DataSourceType;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.db.domain.SpecialCostomInfo;
import com.ruoyi.db.domain.SpecialCostomItemInfo;
import com.ruoyi.db.domain.SpecialCostomManagerInfo;
import com.ruoyi.db.mapper.MemberExtInfoMapper;
import com.ruoyi.db.mapper.SpecialCostomInfoMapper;
import com.ruoyi.db.mapper.SpecialCostomItemInfoMapper;
import com.ruoyi.db.mapper.SpecialCostomManagerInfoMapper;
import com.ruoyi.market.utils.SysNumberUtil;
import com.ruoyi.special.enums.InfoTypeEnum;
import com.ruoyi.special.model.param.QingYuParam;
import com.ruoyi.special.service.ISpecialCostomInfoService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.math.BigDecimal;
import java.time.LocalDate;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 特殊渠道信息Service业务层处理
 * 
 * @author ruoyi
 * @date 2022-08-15
 */
@Service
@DataSource(DataSourceType.SLAVE)
public class SpecialCostomInfoServiceImpl implements ISpecialCostomInfoService 
{

    @Resource
    private SpecialCostomInfoMapper specialCostomInfoMapper;
    @Resource
    MemberExtInfoMapper memberExtInfoMapper;
    @Resource
    private SpecialCostomManagerInfoMapper specialCostomManagerInfoMapper;
    @Autowired
    private SpecialCostomItemInfoMapper specialCostomItemInfoMapper;

    /**
     * 查询特殊渠道信息
     *
     * @param id 特殊渠道信息主键
     * @return 特殊渠道信息
     */
    @Override
    public SpecialCostomInfo selectSpecialCostomInfoById(String id)
    {
        return specialCostomInfoMapper.selectSpecialCostomInfoById(id);
    }

    /**
     * 查询特殊渠道信息列表
     *
     * @param specialCostomInfo 特殊渠道信息
     * @return 特殊渠道信息
     */
    @Override
    public List<SpecialCostomInfo> selectSpecialCostomInfoList(SpecialCostomInfo specialCostomInfo)
    {
        return specialCostomInfoMapper.selectSpecialCostomInfoList(specialCostomInfo);
    }

    /**
     * 查询特殊渠道信息列表
     *
     * @param specialCostomInfo 特殊渠道信息
     * @return 特殊渠道信息
     */
    @Override
    public List<Map<String, Object>> selectSpecialCostomInfoListMap(SpecialCostomInfo specialCostomInfo)
    {
        return specialCostomInfoMapper.selectSpecialCostomInfoListMap(specialCostomInfo);
    }

    @Override
    public List<Map<String, Object>> selectSpecialCostomInfoMineListMap(SpecialCostomInfo specialCostomInfo)
    {
        return specialCostomInfoMapper.selectSpecialCostomInfoMineListMap(specialCostomInfo);
    }

    /**
     * 新增特殊渠道信息
     *
     * @param specialCostomInfo 特殊渠道信息
     * @return 结果
     */
    @Override
    public int insertSpecialCostomInfo(SpecialCostomInfo specialCostomInfo)
    {

        SpecialCostomManagerInfo specialCostomManagerInfo =
                specialCostomManagerInfoMapper.selectSpecialCostomManagerInfoByUserId(specialCostomInfo.getOwnerUserId());
        specialCostomInfo.setOwnerUsername(specialCostomManagerInfo.getUsername());

//        if (StrUtil.equals(specialCostomInfo.getApiType(), QingYuParam.apiType) ||
//                StrUtil.equals(specialCostomInfo.getInfoType(), InfoTypeEnum.API.getValue())){
//            specialCostomInfo.setFactoryType(QingYuParam.apiType);
//            specialCostomInfo.setApiCode(QingYuParam.appCode);
//            specialCostomInfo.setApiSecret(QingYuParam.appSecret);
//            specialCostomInfo.setApiMatchUrl(QingYuParam.checkUrl);
//            specialCostomInfo.setApiRegisterUrl(QingYuParam.pushUrl);
//        }

        specialCostomInfo.setCreateTime(DateUtils.getNowDate());
        return specialCostomInfoMapper.insertSpecialCostomInfo(specialCostomInfo);
    }

    /**
     * 修改特殊渠道信息
     *
     * @param specialCostomInfo 特殊渠道信息
     * @return 结果
     */
    @Override
    public int updateSpecialCostomInfo(SpecialCostomInfo specialCostomInfo)
    {
        if (ObjectUtil.isNotEmpty(specialCostomInfo.getOwnerUserId())){
            SpecialCostomManagerInfo specialCostomManagerInfo =
                    specialCostomManagerInfoMapper.selectSpecialCostomManagerInfoByUserId(specialCostomInfo.getOwnerUserId());
            specialCostomInfo.setOwnerUsername(specialCostomManagerInfo.getUsername());
        }

        return specialCostomInfoMapper.updateSpecialCostomInfo(specialCostomInfo);
    }

    /**
     * 批量删除特殊渠道信息
     *
     * @param ids 需要删除的特殊渠道信息主键
     * @return 结果
     */
    @Override
    public int deleteSpecialCostomInfoByIds(String ids)
    {

        SpecialCostomInfo specialCostomInfo = new SpecialCostomInfo();
        specialCostomInfo.setId(Long.valueOf(ids));
        specialCostomInfo.setStatus(Long.valueOf(0));
        specialCostomInfo.setDelFlag(Long.valueOf(1));
        specialCostomInfo.setUpdateTime(new Date());

        int i = specialCostomInfoMapper.updateSpecialCostomInfo(specialCostomInfo);

        SpecialCostomItemInfo costomItemInfo = new SpecialCostomItemInfo();
        costomItemInfo.setInfoId(Long.valueOf(ids));
        costomItemInfo.setStatus(Integer.valueOf(0));
        int i1 = specialCostomItemInfoMapper.updateByInfoId(costomItemInfo);

        return i;
    }

    @Override
    public SpecialCostomInfo getAccountInfoByDeptId(Long deptId) {
        return specialCostomInfoMapper.getEntryByDeptIdLock(deptId);
    }


    /**
     * 查询机构消耗的条数 和今天
     * @param type 0 今天；1 昨天；2 前天
     * @return
     */
    @Override
    public List<Map<String, Object>> getDataSortOrganizeConsume(Integer type) {

        LocalDate localDate = LocalDate.now().minusDays(type);
        String dateInfo = localDate.toString();

        List<Map<String, Object>> resulltMap = specialCostomInfoMapper.getPushDataMapByDateInfo(dateInfo);

        final BigDecimal[] amount = {BigDecimal.ZERO};
        Long totalNumber = memberExtInfoMapper.countAuthNumber(dateInfo);
        resulltMap.stream().forEach(map -> {
            Long number=  (Long) map.get("number");
            BigDecimal totalAmount=  (BigDecimal) map.get("totalAmount");
            map.put("totalAmount", "¥："+totalAmount);
            map.put("rate", SysNumberUtil.getPointNumber(new BigDecimal(number), new BigDecimal(totalNumber), 4 ));

            amount[0] = amount[0].add(totalAmount);
        });

        Map<String, Object> totalMap = new HashMap<>();
        totalMap.put("name", "全国");
        totalMap.put("number", totalNumber);
        totalMap.put("rate", "100");
        totalMap.put("totalAmount", "¥："+amount[0]);

        resulltMap.add(0, totalMap);
        return resulltMap;
    }

    /**
     *
     * @param type
     * @return
     */
    @Override
    public List<Map<String, Object>> getDataSortOrganizeApi(Integer type) {

        LocalDate localDate = LocalDate.now().minusDays(type);
        String dateInfo = localDate.toString();

        List<Map<String, Object>> resulltMap = specialCostomInfoMapper.getApiPushDataMapByDateInfo(dateInfo);

        final BigDecimal[] amount = {BigDecimal.ZERO};
        Long totalNumber = memberExtInfoMapper.countAuthNumber(dateInfo);
        resulltMap.stream().forEach(map -> {
            Long number=  (Long) map.get("number");
            BigDecimal totalAmount=  (BigDecimal) map.get("totalAmount");
            map.put("totalAmount", "¥："+totalAmount);
            map.put("rate", SysNumberUtil.getPointNumber(new BigDecimal(number), new BigDecimal(totalNumber), 4 ));

            amount[0] = amount[0].add(totalAmount);
        });

        Map<String, Object> totalMap = new HashMap<>();
        totalMap.put("name", "全国");
        totalMap.put("number", totalNumber);
        totalMap.put("rate", "100");
        totalMap.put("totalAmount", "¥："+amount[0]);

        resulltMap.add(0, totalMap);
        return resulltMap;
    }

    @Override
    public List<Map<String, Object>> getAbleCostomInfoMapList() {
        return specialCostomInfoMapper.getAbleCostomInfoMapList();
    }
}
